var  flag = false
function main() {

  //  com.youzu.shaoxiagame.aligames
    // console.log(JSON.stringify(Process.enumerateModules()));
    var libnative_addr = Module.findBaseAddress("libhegame.so")
    console.log("libnative_addr is: " + libnative_addr)

    // var luaL_loadbufferx_addr = Module.findExportByName("libtolua.so",  "luaL_loadbufferx")
    //var luaL_loadbufferx_addr = libnative_addr.add(0x5A9F84)//Module.findExportByName("libhegame.so",  "luaL_loadbufferx")
    var lua_pcall_addr = Module.findExportByName("libtolua.so",  "lua_pcall")
 
    //console.log("luaL_loadbufferx is: " + luaL_loadbufferx_addr)
    if (libnative_addr!=null){
        flag = true
        Interceptor.attach(libnative_addr.add(0x5A9F84), {
            onEnter: function(args) {
                var name = Memory.readCString(args[3])
                console.log("--->"+name)
                if(name.indexOf("lua")!=-1 ){
                    // console.log("--->"+args[0])

                    name = name.replaceAll("/",".")
                    var file = new File('/sdcard/zzzz/'+name+'.lua', 'w')
                    var data = args[1]
                    var datalen = args[2].toInt32()
                    file.write(data.readByteArray(datalen))
                    file.flush();
                    file.close();
                    //  console.log("--->"+name)
                }

            },
            onLeave: function(retval) {

            }
        })
    }


}

setInterval(function(){
    if (!flag)
    main()
}, 1000)



function test() {
    var luaL_loadbufferx_addr = Module.findExportByName("libtolua.so",  "luaL_loadbufferx")
    var lua_pcall_addr = Module.findExportByName("libslua.so",  "lua_pcall")

    var luaL_loadbufferx = new NativeFunction(luaL_loadbufferx_addr , "int", ["pointer", "pointer" , "int", "pointer"])
    var lua_pcall = new NativeFunction(lua_pcall_addr , "int", ["pointer","int","int","int"])
    if( true){
 
         var luastr = "local CombatView = require \"mod/combat/combat_view\"\n"
         +"print(CombatView)\n"
         +"CombatView.CheckCanSkip = function(self, ...)\n"
         +"\tlocal msg = \"skip11111\"\n"
         +"\treturn true,msg\n"
         +"end\n"
         +" \n"
         +"CombatView.CheckShowSkipBtn = function(self, ...)\n"
         +"\treturn true\n"
         +"end"
         console.log(luastr)
         var strName = "hello.lua"
  
         var lua = Memory.allocUtf8String(luastr)
         var size = luastr.length
         var luaName = Memory.allocUtf8String(strName);



         var lua_State = 0xe0ac31c0

         console.log(ptr(lua_State))
         console.log(lua)
         console.log(size)
         console.log(luaName)
     
         luaL_loadbufferx(ptr(lua_State),(lua),size,(luaName))
         lua_pcall(ptr(lua_State),0,0,0)
         console.log("注入lua hook")
    }
}


//frida -U rongyaozg.aligames -l ryzg.js
//frida -U com.kaiqigu.dancer.uc -l ryzg.js
//frida -U com.cmge.xyjjzy.uc -l ryzg.js
//frida -U com.fcjflsdk.yx7477 -l ryzg.js
//frida -U com.youzu.shaoxiagame.aligames -l c2ddumplua.js
//frida -U com.xy51.sn -l c2ddumplua.js
//frida -U --no-pause -f  com.youzu.snxyj.x7sy -l c2ddumplua.js
//frida -U --no-pause -f  com.happyelements.AndroidAnimal.qq -l c2ddumplua.js
//frida -U com.happyelements.AndroidAnimal.qq -l c2ddumplua.js


